/* 
	This dofile runs the regressions. The estimate saving code underneath each regression links their output to the results displayed in the paper. 

	cdo "F:\MonPol Project\ReplicationPackage\Dofiles\a2_RegressionsDispersion.do"
*/
* Technicalities
** Parameters
global sample "_10pct" 	// _10pct _10pctB or _sample
global table5_10_11_14_16_17 ""
global figure8 ""
global table9 ""
global table12 ""
global table13 ""
global figure_20_21 ""
global table15 "1"

global rootL "F:\MonPol Project\ReplicationPackage\Large"
global rootD "F:\MonPol Project\ReplicationPackage"

** Programs
qui do "$rootD/Dofiles/999_Paths"
qui do "$rootD/Dofiles/998_Programs"

cap log close log
log using "$path_logs/a2_RegressionsDispersion.smcl", append smcl name(log)

**# table5
if "$table5_10_11_14_16_17" == "1" {
    * Start log
    cap log close table5_10
    log using "$path_logs/table5_10_11_14_16_17.log", append text name(table5_10_11_14_16_17)
	cap mkdir "$rootD/savedEstimations"
	
	* Load data
	use "$path_regressionData/allResetCompPriceAgePrices_PRF${sample}_prepped", replace
	
	* Generate dummies
	gen L_x_PRF_pos = L_x_PRF >= 0 if L_x_PRF != .
	gen L_xCompDem_PRF_pos = L_xCompDem_PRF >= 0 if L_xCompDem_PRF != .
	
	* Generate indicators
	gen D_PRF_up_h24 = 1 		if D_PRF_h24 == 1
	replace D_PRF_up_h24 = 0 	if inlist(D_PRF_h24, 0, -1)
	
	gen D_PRF_down_h24 = 1 	if D_PRF_h24 == -1
	replace D_PRF_down_h24 = 0 if inlist(D_PRF_h24, 0, 1)
	error 999
	
	* Regression By Table
	** Table 5	
	reghdfe D_PRF_up_h24	L_xCompDem_PRF ebpnew c.L_xCompDem_PRF#c.ebpnew_residual L_xLnPriceAge L*_lip L*_lcpi L*_gs1 L*_ebpnew gs1 lip lcpi [aw=weight], absorb(id_nr month12) cluster(month category) poolsize(8)
	estimates save "$rootD/savedEstimations/1066u_h24${sample}", replace
	estimates save "$rootD/savedEstimations/Table5_C1${sample}", replace
	estimates save "$rootD/savedEstimations/Table9_C1${sample}", replace
	estimates save "$rootD/savedEstimations/Table14_C1${sample}", replace
	estimates save "$rootD/savedEstimations/Table15_C1${sample}", replace
	estimates save "$rootD/savedEstimations/Table16_C1${sample}", replace
	estimates save "$rootD/savedEstimations/Table17_C1${sample}", replace
	reghdfe D_PRF_up_h24	L_xCompDem_PRF c.L_xCompDem_PRF#c.ebpnew_residual L_xLnPriceAge [aw=weight], absorb(id_nr month) cluster(month category) poolsize(8)
	estimates save "$rootD/savedEstimations/1067u_h24${sample}", replace
	estimates save "$rootD/savedEstimations/Table5_C2${sample}", replace
	reghdfe D_PRF_up_h24	i.L_xCompDem_PRF_pos#(c.L_xCompDem_PRF c.L_xCompDem_PRF#c.ebpnew_residual) ebpnew L_xLnPriceAge L*_lip L*_lcpi L*_gs1 L*_ebpnew gs1 lip lcpi [aw=weight], absorb(id_nr month12) cluster(month category) poolsize(8)
	estimates save "$rootD/savedEstimations/1068u_h24${sample}", replace
	estimates save "$rootD/savedEstimations/Table5_C3${sample}", replace
	
	
	reghdfe D_PRF_down_h24	L_xCompDem_PRF ebpnew c.L_xCompDem_PRF#c.ebpnew_residual L_xLnPriceAge L*_lip L*_lcpi L*_gs1 L*_ebpnew gs1 lip lcpi [aw=weight], absorb(id_nr month12) cluster(month category) poolsize(8)
	estimates save "$rootD/savedEstimations/1066d_h24${sample}", replace
	estimates save "$rootD/savedEstimations/Table5_C4${sample}", replace
	estimates save "$rootD/savedEstimations/Table9_C3${sample}", replace
	estimates save "$rootD/savedEstimations/Table14_C3${sample}", replace
	estimates save "$rootD/savedEstimations/Table15_C3${sample}", replace
	estimates save "$rootD/savedEstimations/Table16_C3${sample}", replace
	estimates save "$rootD/savedEstimations/Table17_C3${sample}", replace
	reghdfe D_PRF_down_h24	L_xCompDem_PRF c.L_xCompDem_PRF#c.ebpnew_residual L_xLnPriceAge [aw=weight], absorb(id_nr month) cluster(month category) poolsize(8)
	estimates save "$rootD/savedEstimations/1067d_h24${sample}", replace
	estimates save "$rootD/savedEstimations/Table5_C5${sample}", replace
	reghdfe D_PRF_down_h24	i.L_xCompDem_PRF_pos#(c.L_xCompDem_PRF c.L_xCompDem_PRF#c.ebpnew_residual) ebpnew L_xLnPriceAge L*_lip L*_lcpi L*_gs1 L*_ebpnew gs1 lip lcpi [aw=weight], absorb(id_nr month12) cluster(month category) poolsize(8)
	estimates save "$rootD/savedEstimations/1068d_h24${sample}", replace
	estimates save "$rootD/savedEstimations/Table5_C6${sample}", replace

	
	** Table 10: Estimates, scanner data, reset-price gap, credit shock
	reghdfe D_PRF_up_h24 	L_x_PRF ebpnew c.L_x_PRF#c.ebpnew_residual L_xLnPriceAge L*_lip L*_lcpi L*_gs1 L*_ebpnew gs1 lip lcpi [aw=weight], absorb(id_nr month12) cluster(month category) poolsize(8)
	estimates save "$rootD/savedEstimations/66u_h24${sample}", replace
	estimates save "$rootD/savedEstimations/Table10_C1${sample}", replace
	reghdfe D_PRF_up_h24 	L_x_PRF c.L_x_PRF#c.ebpnew_residual L_xLnPriceAge [aw=weight], absorb(id_nr month) cluster(month category) poolsize(8)
	estimates save "$rootD/savedEstimations/67u_h24${sample}", replace
	estimates save "$rootD/savedEstimations/Table10_C2${sample}", replace
	reghdfe D_PRF_up_h24 	L_x_PRF_pos#(c.L_x_PRF c.L_x_PRF#c.ebpnew_residual) ebpnew L_xLnPriceAge L*_lip L*_lcpi L*_gs1 L*_ebpnew gs1 lip lcpi [aw=weight], absorb(id_nr month12) cluster(month category) poolsize(8)
	estimates save "$rootD/savedEstimations/68u_h24${sample}", replace
	estimates save "$rootD/savedEstimations/Table10_C3${sample}", replace
	
	reghdfe D_PRF_down_h24 L_x_PRF ebpnew c.L_x_PRF#c.ebpnew_residual L_xLnPriceAge L*_lip L*_lcpi L*_gs1 L*_ebpnew gs1 lip lcpi [aw=weight], absorb(id_nr month12) cluster(month category) poolsize(8)
	estimates save "$rootD/savedEstimations/66d_h24${sample}", replace
	estimates save "$rootD/savedEstimations/Table10_C4${sample}", replace
	reghdfe D_PRF_down_h24 L_x_PRF c.L_x_PRF#c.ebpnew_residual L_xLnPriceAge [aw=weight], absorb(id_nr month) cluster(month category) poolsize(8)
	estimates save "$rootD/savedEstimations/67d_h24${sample}", replace
	estimates save "$rootD/savedEstimations/Table10_C5${sample}", replace
	reghdfe D_PRF_down_h24 L_x_PRF_pos#(c.L_x_PRF c.L_x_PRF#c.ebpnew_residual) ebpnew L_xLnPriceAge L*_lip L*_lcpi L*_gs1 L*_ebpnew gs1 lip lcpi [aw=weight], absorb(id_nr month12) cluster(month category) poolsize(8)
	estimates save "$rootD/savedEstimations/68d_h24${sample}", replace
	estimates save "$rootD/savedEstimations/Table10_C6${sample}", replace

	
	** Table 11: Estimates, scanner data, competitor-price gap, monetary policy shock
	reghdfe D_PRF_up_h24	L_xCompDem_PRF FF4_alt1_norm c.L_xCompDem_PRF#c.FF4_alt1_norm L_xLnPriceAge L*_lip L*_lcpi L*_gs1 L*_ebpnew gs1 lip lcpi [aw=weight], absorb(id_nr month12) cluster(month category) poolsize(8)
	estimates save "$rootD/savedEstimations/Table11_C1${sample}", replace
	reghdfe D_PRF_up_h24	L_xCompDem_PRF c.L_xCompDem_PRF#c.FF4_alt1_norm L_xLnPriceAge [aw=weight], absorb(id_nr month) cluster(month category) poolsize(8)
	estimates save "$rootD/savedEstimations/Table11_C2${sample}", replace
	reghdfe D_PRF_up_h24	i.L_xCompDem_PRF_pos#(c.L_xCompDem_PRF c.L_xCompDem_PRF#c.FF4_alt1_norm) FF4_alt1_norm L_xLnPriceAge L*_lip L*_lcpi L*_gs1 L*_ebpnew gs1 lip lcpi [aw=weight], absorb(id_nr month12) cluster(month category) poolsize(8)
	estimates save "$rootD/savedEstimations/Table11_C3${sample}", replace
		
	reghdfe D_PRF_down_h24	L_xCompDem_PRF FF4_alt1_norm c.L_xCompDem_PRF#c.FF4_alt1_norm L_xLnPriceAge L*_lip L*_lcpi L*_gs1 L*_ebpnew gs1 lip lcpi [aw=weight], absorb(id_nr month12) cluster(month category) poolsize(8)
	estimates save "$rootD/savedEstimations/Table11_C4${sample}", replace
	reghdfe D_PRF_down_h24	L_xCompDem_PRF c.L_xCompDem_PRF#c.FF4_alt1_norm L_xLnPriceAge [aw=weight], absorb(id_nr month) cluster(month category) poolsize(8)
	estimates save "$rootD/savedEstimations/Table11_C5${sample}", replace
	reghdfe D_PRF_down_h24	i.L_xCompDem_PRF_pos#(c.L_xCompDem_PRF c.L_xCompDem_PRF#c.FF4_alt1_norm) FF4_alt1_norm L_xLnPriceAge L*_lip L*_lcpi L*_gs1 L*_ebpnew gs1 lip lcpi [aw=weight], absorb(id_nr month12) cluster(month category) poolsize(8)
	estimates save "$rootD/savedEstimations/Table11_C6${sample}", replace
	
	** Table 14: Robustness to dropping item fixed effects, scanner data, competitors' price gap, credit shock
	reghdfe D_PRF_up_h24	L_xCompDem_PRF ebpnew c.L_xCompDem_PRF#c.ebpnew_residual L_xLnPriceAge L*_lip L*_lcpi L*_gs1 L*_ebpnew gs1 lip lcpi [aw=weight], absorb(month12) cluster(month category) poolsize(8)
	estimates save "$rootD/savedEstimations/Table14_C2${sample}", replace
	
	reghdfe D_PRF_down_h24	L_xCompDem_PRF ebpnew c.L_xCompDem_PRF#c.ebpnew_residual L_xLnPriceAge L*_lip L*_lcpi L*_gs1 L*_ebpnew gs1 lip lcpi [aw=weight], absorb(month12) cluster(month category) poolsize(8)
	estimates save "$rootD/savedEstimations/Table14_C4${sample}", replace
	
	** Table 16: Robustness using product-months with at least 50 competitors, scanner data, competitor-price gap, credit shock
	merge 1:1 id_nr_unique month using "$path_regressionData/allHybridPrices_DPRF", keep(match master) keepusing(count_PRF) nogen
	reghdfe D_PRF_up_h24	L_xCompDem_PRF ebpnew c.L_xCompDem_PRF#c.ebpnew_residual L_xLnPriceAge L*_lip L*_lcpi L*_gs1 L*_ebpnew gs1 lip lcpi [aw=weight] if count_PRF >= 50, absorb(id_nr month12) cluster(month category) poolsize(8)
	estimates save "$rootD/savedEstimations/Table16_C2${sample}", replace
	reghdfe D_PRF_down_h24	L_xCompDem_PRF ebpnew c.L_xCompDem_PRF#c.ebpnew_residual L_xLnPriceAge L*_lip L*_lcpi L*_gs1 L*_ebpnew gs1 lip lcpi [aw=weight] if count_PRF >= 50, absorb(id_nr month12) cluster(month category) poolsize(8)
	estimates save "$rootD/savedEstimations/Table16_C4${sample}", replace
	
	** Table 17
	reghdfe D_PRF_up_h24	L_xCompDem_PRF ebpnew c.L_xCompDem_PRF#c.ebpnew_residual L_xLnPriceAge L*_lip L*_lcpi L*_gs1 L*_ebpnew gs1 lip lcpi [aw=weight] if inrange(year, 2001, 2007), absorb(id_nr month12) cluster(month category) poolsize(8)
	estimates save "$rootD/savedEstimations/Table17_C2${sample}", replace
	reghdfe D_PRF_down_h24	L_xCompDem_PRF ebpnew c.L_xCompDem_PRF#c.ebpnew_residual L_xLnPriceAge L*_lip L*_lcpi L*_gs1 L*_ebpnew gs1 lip lcpi [aw=weight] if inrange(year, 2001, 2007), absorb(id_nr month12) cluster(month category) poolsize(8)
	estimates save "$rootD/savedEstimations/Table17_C4${sample}", replace
	
    * Close log
    log close table5_10_11_14_16_17
}


**# figure8
if "$figure8" == "1" {
    * Start log
    cap log close figure8
    log using "$path_logs/figure8.log", append text name(figure8)
	cap mkdir "$path_graphs/regressionGraphs"
	
	* Load data
	use "$path_regressionData/allResetCompPriceAgePrices_PRF${sample}_prepped", replace
	drop L*_gs1 L*_ebpnew L*_lip L*_lcpi L*_lppi* 
	drop _merge ff gs1 ebpnew lip lcpi lppi*
	
	* Generate dummies
	gquantiles posDummy = L_xCompDem_PRF [aw=weight] if L_xCompDem_PRF > 0, xtile nquantiles(15)
	gquantiles negDummy = L_xCompDem_PRF [aw=weight] if L_xCompDem_PRF <= 0, xtile nquantiles(15)
	gen 	gapDummy_compDem = -7 if inlist(negDummy, 1, 2)
	replace gapDummy_compDem = -6 if inlist(negDummy, 3, 4)
	replace gapDummy_compDem = -5 if inlist(negDummy, 5, 6)
	replace gapDummy_compDem = -4 if inlist(negDummy, 7, 8)
	replace gapDummy_compDem = -3 if inlist(negDummy, 9, 10)
	replace gapDummy_compDem = -2 if inlist(negDummy, 11, 12)
	replace gapDummy_compDem = -1 if inlist(negDummy, 13, 14)
	replace gapDummy_compDem = 0 	if negDummy == 15 | posDummy == 1
	replace gapDummy_compDem = 1 	if inlist(posDummy, 2, 3)
	replace gapDummy_compDem = 2 	if inlist(posDummy, 4, 5)
	replace gapDummy_compDem = 3 	if inlist(posDummy, 6, 7)
	replace gapDummy_compDem = 4 	if inlist(posDummy, 8, 9)
	replace gapDummy_compDem = 5 	if inlist(posDummy, 10, 11)
	replace gapDummy_compDem = 6 	if inlist(posDummy, 12, 13)
	replace gapDummy_compDem = 7 	if inlist(posDummy, 14, 15)
	drop posDummy negDummy
	bysort gapDummy_compDem: sum L_xCompDem_PRF
		
	* Prep for regression
	forvalues i = -7(1)7 {
		di `i'
		** Negative gap groups
		if `i' < 0 {
			local j = abs(`i')
			gen byte gapDummy_comp_M`j' = (gapDummy_compDem == `i')			
			gen compXebp_M`j' = (gapDummy_compDem == `i') * ebpnew_residual 
		}
		** Positive gap groups
		if `i' > 0 {
			gen byte gapDummy_comp_P`i' = (gapDummy_compDem == `i')
			gen compXebp_P`i' = (gapDummy_compDem == `i') * ebpnew_residual 
		}
		** Middle group
		if `i' == 0 {
			gen byte gapDummy_comp_`i' = (gapDummy_compDem == `i')
			gen compXebp_`i' = (gapDummy_compDem == `i') * ebpnew_residual 
		}
	}
	gen D_PRF_up_h24 = 1 		if D_PRF_h24 == 1
	replace D_PRF_up_h24 = 0 	if inlist(D_PRF_h24, 0, -1)
	
	*** Prices were decreased
	gen D_PRF_down_h24 = 1 	if D_PRF_h24 == -1
	replace D_PRF_down_h24 = 0 if inlist(D_PRF_h24, 0, 1)
	
	* Figure 8 regressions
	reghdfe D_PRF_up_h24 gapDummy_comp_M* gapDummy_comp_P*  compXebp_M* compXebp_P* L_xLnPriceAge, absorb(id_nr month) cluster(month category) poolsize(10)
	estimates save "$rootD/savedEstimations/1079u_h24${sample}", replace
	reghdfe D_PRF_down_h24 gapDummy_comp_M* gapDummy_comp_P*  compXebp_M* compXebp_P* L_xLnPriceAge, absorb(id_nr month) cluster(month category) poolsize(10)
	estimates save "$rootD/savedEstimations/1079d_h24${sample}", replace
	drop D_PRF_up_h24 D_PRF_down_h24
	
	* Figure 8 graph
	** Identify estimate files
	local estimateFiles ""
	local estimateFiles1079 : dir "$rootD/savedEstimations/" files "1079_*.ster", respectcase
	local estimateFiles1079u : dir "$rootD/savedEstimations/" files "1079u_*.ster", respectcase
	local estimateFiles1079d : dir "$rootD/savedEstimations/" files "1079d_*.ster", respectcase
	local estimateFiles1079a : dir "$rootD/savedEstimations/" files "1079a_*.ster", respectcase
	local estimateFiles1079any : dir "$rootD/savedEstimations/" files "1079any_*.ster", respectcase
	local estimateFiles : list  estimateFiles  | estimateFiles1079
	local estimateFiles : list  estimateFiles  | estimateFiles1079u
	local estimateFiles : list  estimateFiles  | estimateFiles1079d
	local estimateFiles : list  estimateFiles  | estimateFiles1079a
	local estimateFiles : list  estimateFiles  | estimateFiles1079any
	
	** Store estimates
	cwf default
	cap frame drop temp
	foreach estimateFile of local estimateFiles {
		estimates use "$rootD/savedEstimations/`estimateFile'"
		local newName = subinstr("`estimateFile'", ".ster", "", .)
		_eststo e`newName'
		qui estimates replay e`newName'
		postresults, frame("temp") selection("1 gap, 15 selection") extra("e`newName'/-7")
		postresults, frame("temp") selection("2 gap, 16 selection") extra("e`newName'/-6")
		postresults, frame("temp") selection("3 gap, 17 selection") extra("e`newName'/-5")
		postresults, frame("temp") selection("4 gap, 18 selection") extra("e`newName'/-4")
		postresults, frame("temp") selection("5 gap, 19 selection") extra("e`newName'/-3")
		postresults, frame("temp") selection("6 gap, 20 selection") extra("e`newName'/-2")
		postresults, frame("temp") selection("7 gap, 21 selection") extra("e`newName'/-1")
		postresults, frame("temp") selection("8 gap, 22 selection") extra("e`newName'/1")
		postresults, frame("temp") selection("9 gap, 23 selection") extra("e`newName'/2")
		postresults, frame("temp") selection("10 gap, 24 selection") extra("e`newName'/3")
		postresults, frame("temp") selection("11 gap, 25 selection") extra("e`newName'/4")
		postresults, frame("temp") selection("12 gap, 26 selection") extra("e`newName'/5")
		postresults, frame("temp") selection("13 gap, 27 selection") extra("e`newName'/6")
		postresults, frame("temp") selection("14 gap, 28 selection") extra("e`newName'/7")
	}
	
		** Plot
		cwf temp
		split extra, parse("/")
		drop extra
		keep if inlist(extra1, "e1079d_h24_10pct", "e1079u_h24_10pct")
		rename extra1 type
		destring extra2, replace
		rename extra2 group
		label var group "Gap-size based group"
		label var gap_b "Competitor gap (left axis)"
		label var selection_b "Selection (right axis)"
		
		insobs 1
		replace group = 0 if missing(group)
		replace type = "e1079d_h24_10pct" if missing(type)
		
		expand 2 in l
		replace type = "e1079u_h24_10pct" in l
		
		foreach var of varlist gap_* selection_* {
			replace `var' = `var' * 100
		}
		
		local xValues "-6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7"
		di wordcount("`xValues'")
		local edgePoints "-22 -14 -10 -7 -5 -3 -1 1 3 4 6 9 12 17"
		di wordcount("`edgePoints''")
		labmap ticks, values(`xValues') labels(`edgePoints')
		
		replace group = group + 0.5
		gen group2 = group + 0.1
		label values group2 ticks
		
		sort group
		twoway (rcap gap_ll gap_ul group if type == "e1079u_h24_10pct", lcolor(maroon%50) lwidth(medthick) msize(vtiny)) ///
		(rcap selection_ll selection_ul group2 if type == "e1079u_h24_10pct", yaxis(2) lcolor(navy%50) lwidth(medthick) msize(vtiny)) ///
		(scatter gap_b group if type == "e1079u_h24_10pct", lcolor(maroon) mcolor(maroon) msymbol(Oh) connect(ascending) cmissing(n)) ///
		(scatter selection_b group2 if type == "e1079u_h24_10pct", yaxis(2) lcolor(navy) mcolor(navy) msymbol(Dh) connect(ascending) cmissing(n)) ///
		, xlabel(-7(1)7, notick nolabel) xmtick(-6(1)7, tlength(*2) labsize(medium) labels grid glcolor(black%5) valuelabel) ylabel(-50(25)50, axis(1) grid glcolor(black%5)) ylabel(-5(2.5)5, axis(2)) legend(order(2 4) row(2) ring(0) pos(6)) graphregion(color(white)) ytitle("Impact on probability of price adjustment" "in percentage points", axis(1) margin(right)) xtitle("Competitor based gap (in %)", margin(top)) yline(0, lcolor(black%40) lpattern(dash))
		graphsave3 $path_graphs/regressionGraphs/1079u_15groups
		graphsave3 $path_graphs/regressionGraphs/Figure8_PriceIncreases
		
		twoway (rcap gap_ll gap_ul group if type == "e1079d_h24_10pct", lcolor(maroon%50) lwidth(medthick) msize(vtiny)) ///
		(rcap selection_ll selection_ul group2 if type == "e1079d_h24_10pct", yaxis(2) lcolor(navy%50) lwidth(medthick) msize(vtiny)) ///
		(scatter gap_b group if type == "e1079d_h24_10pct", lcolor(maroon) mcolor(maroon) msymbol(Oh) connect(ascending) cmissing(n)) ///
		(scatter selection_b group2 if type == "e1079d_h24_10pct", yaxis(2) lcolor(navy) mcolor(navy) msymbol(Dh) connect(ascending) cmissing(n)) ///
		, xlabel(-7(1)7, notick nolabel) xmtick(-6(1)7, tlength(*2) labsize(medium) labels grid glcolor(black%5) valuelabel) ylabel(-50(25)50, axis(1) grid glcolor(black%5)) ylabel(-5(2.5)5, axis(2)) legend(order(2 4) row(2) ring(0) pos(6)) graphregion(color(white)) ytitle("Impact on probability of price adjustment" "in percentage points", axis(1) margin(right)) xtitle("Competitor based gap (in %)", margin(top)) yline(0, lcolor(black%40) lpattern(dash))
		graphsave3 $path_graphs/regressionGraphs/1079d_15groups
		graphsave3 $path_graphs/regressionGraphs/Figure8_PriceDecreases


    * Close log
    log close figure8
}


**# table9
if "$table9" == "1" {
    * Start log
    cap log close table9
    log using "$path_logs/table9.log", append text name(table9)
    
	* Load data
	use "$path_regressionData/allResetCompPriceAgePrices_PRF${sample}_prepped", replace
	merge 1:1 id_nr_unique month using "$path_regressionData/allHybridPrices_DPRF${sample}", keep(match master) keepusing(count_PRF count_DPRF L_xHybridDem_DPRF) nogen
	
	* Generate indicators
	gen D_PRF_up_h24 = 1 		if D_PRF_h24 == 1
	replace D_PRF_up_h24 = 0 	if inlist(D_PRF_h24, 0, -1)
	
	gen D_PRF_down_h24 = 1 	if D_PRF_h24 == -1
	replace D_PRF_down_h24 = 0 if inlist(D_PRF_h24, 0, 1)
	
	
	* Table 9: Robustness to using competitor-reset gap, scanner data, credit shock
	reghdfe D_PRF_up_h24	c.L_xHybridDem_DPRF ebpnew c.L_xHybridDem_DPRF#c.ebpnew_residual L_xLnPriceAge L*_lip L*_lcpi L*_gs1 L*_ebpnew gs1 lip lcpi if count_DPRF >= 50, absorb(id_nr month12) cluster(month category)
	estimates save "$rootD/savedEstimations/1098u_t50h24${sample}", replace
	estimates save "$rootD/savedEstimations/Table9_C2${sample}", replace
	
	reghdfe D_PRF_down_h24	c.L_xHybridDem_DPRF ebpnew c.L_xHybridDem_DPRF#c.ebpnew_residual L_xLnPriceAge L*_lip L*_lcpi L*_gs1 L*_ebpnew gs1 lip lcpi if count_DPRF >= 50, absorb(id_nr month12) cluster(month category)
	estimates save "$rootD/savedEstimations/1098d_t50_h24${sample}", replace
	estimates save "$rootD/savedEstimations/Table9_C4${sample}", replace
	
	
    * Close log
    log close table9
}

**# table12
if "$table12" == "1" {
    * Start log
    cap log close table12
    log using "$path_logs/table12.log", append text name(table12)
	
	* Load data
	use "$path_regressionData/allResetCompPriceAgePrices_PRF${sample}_prepped", replace
	
	* Generate dummies
	** Reset
	gquantiles posDummy = L_x_PRF [aw=weight] if L_x_PRF > 0, xtile nquantiles(5)
	gquantiles negDummy = L_x_PRF [aw=weight] if L_x_PRF <= 0, xtile nquantiles(5)
	gen 	gapDummy_reset = -2 if inlist(negDummy, 1, 2)
	replace gapDummy_reset = -1 if inlist(negDummy, 3, 4)
	replace gapDummy_reset = 0 	if negDummy == 5 | posDummy == 1
	replace gapDummy_reset = 1 	if inlist(posDummy, 2, 3)
	replace gapDummy_reset = 2 	if inlist(posDummy, 4, 5)
	drop posDummy negDummy
	bysort gapDummy_reset: sum L_x_PRF
	
	** Competitor
	gquantiles posDummy = L_xCompDem_PRF [aw=weight] if L_xCompDem_PRF > 0, xtile nquantiles(5)
	gquantiles negDummy = L_xCompDem_PRF [aw=weight] if L_xCompDem_PRF <= 0, xtile nquantiles(5)
	gen 	gapDummy_compDem = -2 if inlist(negDummy, 1, 2)
	replace gapDummy_compDem = -1 if inlist(negDummy, 3, 4)
	replace gapDummy_compDem = 0 	if negDummy == 5 | posDummy == 1
	replace gapDummy_compDem = 1 	if inlist(posDummy, 2, 3)
	replace gapDummy_compDem = 2 	if inlist(posDummy, 4, 5)
	drop posDummy negDummy
	bysort gapDummy_compDem: sum L_xCompDem_PRF

	** Prep for regression
	forvalues i = -2(1)2 {
		di `i'
		** Negative gap groups
		if `i' < 0 {
			local j = abs(`i')
			gen gapDummy_res_M`j' = (gapDummy_reset == `i')
			gen gapDummy_comp_M`j' = (gapDummy_compDem == `i')
			
			gen resXebp_M`j' = (gapDummy_reset == `i') * ebpnew_residual
			gen compXebp_M`j' = (gapDummy_compDem == `i') * ebpnew_residual 
		}
		** Positive gap groups
		if `i' > 0 {
			gen gapDummy_res_P`i' = (gapDummy_reset == `i')
			gen gapDummy_comp_P`i' = (gapDummy_compDem == `i')
			
			gen resXebp_P`i' = (gapDummy_reset == `i') * ebpnew_residual
			gen compXebp_P`i' = (gapDummy_compDem == `i') * ebpnew_residual 
		}
		** Middle group
		if `i' == 0 {
			gen gapDummy_res_`i' = (gapDummy_reset == `i')
			gen gapDummy_comp_`i' = (gapDummy_compDem == `i')
			
			gen resXebp_`i' = (gapDummy_reset == `i') * ebpnew_residual
			gen compXebp_`i' = (gapDummy_compDem == `i') * ebpnew_residual 
		}
	}
	
	
	* Up/down indicators
	** Prices were increased
	gen D_PRF_up_h24 = 1 		if D_PRF_h24 == 1
	replace D_PRF_up_h24 = 0 	if inlist(D_PRF_h24, 0, -1)
	
	** Prices were decreased
	gen D_PRF_down_h24 = 1 	if D_PRF_h24 == -1
	replace D_PRF_down_h24 = 0 if inlist(D_PRF_h24, 0, 1)
	
	* Regressions
	reghdfe D_PRF_up_h24 gapDummy_comp_M* gapDummy_comp_P*  compXebp_M* compXebp_P* L_xLnPriceAge, absorb(id_nr month) cluster(month category) poolsize(20)
	estimates save "$rootD/savedEstimations/1078u_h24${sample}", replace
	estimates save "$rootD/savedEstimations/Table12_C1${sample}", replace
	reghdfe D_PRF_down_h24 gapDummy_comp_M* gapDummy_comp_P*  compXebp_M* compXebp_P* L_xLnPriceAge, absorb(id_nr month) cluster(month category) poolsize(20)
	estimates save "$rootD/savedEstimations/1078d_h24${sample}", replace
	estimates save "$rootD/savedEstimations/Table12_C2${sample}", replace
	drop D_PRF_up_h24 D_PRF_down_h24
    
    * Close log
    log close table12
}


**# table13
if "$table13" == "1" {
    * Start log
    cap log close table13
    log using "$path_logs/table13.log", append text name(table13)

	* Load data
	use "$path_regressionData/allResetCompPriceAgePrices_PRF${sample}_prepped", replace
		
	* Reduce dataset
	keep category id_nr_unique month D_PRF_h24 L_xCompDem_PRF ebpnew ebpnew_residual L_xLnPriceAge L*_lip L*_lcpi L*_gs1 L*_ebpnew gs1 lip lcpi month12	
	label define upNoDown -1 "Down" 0 "No change" 1 "Up", replace
	label values D_PRF_h24 upNoDown
	
	* Merge in frequencies and norm/stand versions of gap
	merge 1:1 id_nr_unique month using "$path_output/frequencyVariables/productMarket${sample}", keep(match master) keepusing(PM_mean_down_h24 PM_mean_up_h24 ID_mean_down_h24 ID_mean_up_h24 L_xCompDem_PRF_norm L_xCompDem_PRF_stand) nogen
	
	* Standardised gaps
	sum L_xCompDem_PRF_stand, d
	local p5 = r(p5)
	local p95 = r(p95)
	gen sample_stand = inrange(L_xCompDem_PRF_stand, `p5', `p95')
	
	* Regressions
	** Multinomial probit (standardised by product-store, with PM control)
	mprobit D_PRF_h24 L_xCompDem_PRF_stand ebpnew c.L_xCompDem_PRF_stand#c.ebpnew_residual L_xLnPriceAge PM_mean_down_h24 PM_mean_up_h24 L*_lip L*_lcpi L*_gs1 L*_ebpnew gs1 lip lcpi i.month12 if sample_stand == 1, baseoutcome(0) vce(cluster month)
	estimates save "$rootD/savedEstimations/Table13_C1_C2${sample}", replace
	estimates save "$rootD/savedEstimations/mprobit_pmf_st${sample}", replace
	
	** Ordered probit (standardised by product-store, with PM control)
	oprobit D_PRF_h24 L_xCompDem_PRF_stand ebpnew c.L_xCompDem_PRF_stand#c.ebpnew_residual L_xLnPriceAge PM_mean_down_h24 PM_mean_up_h24 L*_lip L*_lcpi L*_gs1 L*_ebpnew gs1 lip lcpi i.month12 if sample_stand == 1, vce(cluster month)
	estimates save "$rootD/savedEstimations/oprobit_pmf_st${sample}", replace
	estimates save "$rootD/savedEstimations/Table13_C3${sample}", replace

    * Close log
    log close table13
}


**# figure_20_21
if "$figure_20_21" == "1" {
    * Start log
    cap log close figure_20_21
    log using "$path_logs/figure_20_21.log", append text name(figure_20_21)
	cap mkdir "$path_graphs/categoryRegressions"
	
	* Load combined data
	use "$path_regressionData/allResetCompPriceAgePrices_PRF${sample}_prepped", replace
	
	* Gen up/down
	gen D_PRF_up_h24 = 1 		if D_PRF_h24 == 1
	replace D_PRF_up_h24 = 0 	if inlist(D_PRF_h24, 0, -1)
	
	gen D_PRF_down_h24 = 1 	if D_PRF_h24 == -1
	replace D_PRF_down_h24 = 0 if inlist(D_PRF_h24, 0, 1)
	
	* Regress overall
	reghdfe D_PRF_down_h24	L_xCompDem_PRF ebpnew c.L_xCompDem_PRF#c.ebpnew_residual L_xLnPriceAge L*_lip L*_lcpi L*_gs1 L*_ebpnew gs1 lip lcpi, absorb(id_nr month12) cluster(month category) poolsize(10)
	estimates save "$rootD/savedEstimations/1086d_all_h24${sample}", replace
	reghdfe D_PRF_up_h24	L_xCompDem_PRF ebpnew c.L_xCompDem_PRF#c.ebpnew_residual L_xLnPriceAge L*_lip L*_lcpi L*_gs1 L*_ebpnew gs1 lip lcpi, absorb(id_nr month12) cluster(month category) poolsize(10)
	estimates save "$rootD/savedEstimations/1086u_all_h24${sample}", replace
	
	* Regress by category
	levelsof category, local(categories)
	foreach category of local categories {
		reghdfe D_PRF_down_h24	L_xCompDem_PRF ebpnew c.L_xCompDem_PRF#c.ebpnew_residual L_xLnPriceAge L*_lip L*_lcpi L*_gs1 L*_ebpnew gs1 lip lcpi if category == `category', absorb(id_nr month12) cluster(month) poolsize(10)
		estimates save "$rootD/savedEstimations/1086d_`category'_h24${sample}", replace
		reghdfe D_PRF_up_h24	L_xCompDem_PRF ebpnew c.L_xCompDem_PRF#c.ebpnew_residual L_xLnPriceAge L*_lip L*_lcpi L*_gs1 L*_ebpnew gs1 lip lcpi if category == `category', absorb(id_nr month12) cluster(month) poolsize(10)
		estimates save "$rootD/savedEstimations/1086u_`category'_h24${sample}", replace
	}		
		
	* Load estimates
	clear
	estimates clear
	cwf default
	local estimateFiles : dir "$rootD/savedEstimations/" files "1086*${sample}.ster", respectcase
	foreach estimateFile of local estimateFiles {
		estimates use "$rootD/savedEstimations/`estimateFile'"
		local newName = subinstr("`estimateFile'", ".ster", "", .)
		_eststo e`newName'
	}
	
	* Generate dataset
	cap frame drop resultsFrame
	frame create resultsFrame category str4(direction) gap_b gap_ll gap_ul shock_b shock_ll shock_ul interaction_b interaction_ll interaction_ul
	forvalues i = 1/31 {
		estimates replay e1086d_`i'_h24${sample}
		mat results = r(table)
		frame post resultsFrame (`i') ("down") (results[1,1]) (results[5,1]) (results[6,1]) (results[1,2]) (results[5,2]) (results[6,2]) (results[1,3]) (results[5,3]) (results[6,3]) 		
		estimates replay e1086u_`i'_h24${sample}
		mat results = r(table)
		frame post resultsFrame (`i') ("up") (results[1,1]) (results[5,1]) (results[6,1]) (results[1,2]) (results[5,2]) (results[6,2]) (results[1,3]) (results[5,3]) (results[6,3]) 		
	}
	estimates replay e1086d_all_h24${sample}
	mat results = r(table)
	frame post resultsFrame (0) ("down") (results[1,1]) (results[5,1]) (results[6,1]) (results[1,2]) (results[5,2]) (results[6,2]) (results[1,3]) (results[5,3]) (results[6,3]) 		
	estimates replay e1086u_all_h24${sample}
	mat results = r(table)
	frame post resultsFrame (0) ("up") (results[1,1]) (results[5,1]) (results[6,1]) (results[1,2]) (results[5,2]) (results[6,2]) (results[1,3]) (results[5,3]) (results[6,3]) 		
	
	
	* Plot results
	cwf resultsFrame
	defineCategoryLabel
	label define categoryLabel 0 "all", modify
	label values category categoryLabel
		
	** Up
	meta set gap_b gap_ll gap_ul if direction == "up"
	meta forestplot category _plot _esci, sort(category) nullrefline insidemarker note("Gap up")
	graphsave3 $path_graphs/categoryRegressions/gap_up${sample}
	graphsave3 $path_graphs/categoryRegressions/Figure20_C1${sample}
	
	meta set shock_b shock_ll shock_ul  if direction == "up"
	meta forestplot category _plot _esci, sort(category) nullrefline insidemarker note("Shock up")
	graphsave3 $path_graphs/categoryRegressions/shock_up${sample}
	graphsave3 $path_graphs/categoryRegressions/Figure20_C2${sample}
	
	meta set interaction_b interaction_ll interaction_ul  if direction == "up"
	meta forestplot category _plot _esci, sort(category) nullrefline insidemarker note("Interaction up")
	graphsave3 $path_graphs/categoryRegressions/interaction_up${sample}
	graphsave3 $path_graphs/categoryRegressions/Figure20_C3${sample}
	
	** Down
	meta set gap_b gap_ll gap_ul if direction == "down"
	meta forestplot category _plot _esci, sort(category) nullrefline insidemarker note("Gap down")
	graphsave3 $path_graphs/categoryRegressions/gap_down${sample}
	graphsave3 $path_graphs/categoryRegressions/Figure21_C1${sample}
	
	meta set shock_b shock_ll shock_ul  if direction == "down"
	meta forestplot category _plot _esci, sort(category) nullrefline insidemarker note("Shock down")
	graphsave3 $path_graphs/categoryRegressions/shock_down${sample}
	graphsave3 $path_graphs/categoryRegressions/Figure21_C2${sample}
	
	meta set interaction_b interaction_ll interaction_ul  if direction == "down"
	meta forestplot category _plot _esci, sort(category) nullrefline insidemarker note("Interaction down")
	graphsave3 $path_graphs/categoryRegressions/interaction_down${sample}
	graphsave3 $path_graphs/categoryRegressions/Figure21_C3${sample}
    
    * Close log
    log close figure_20_21
}

**# table15
if "$table15" == "1" {
    * Start log
    cap log close table15
    log using "$path_logs/table15.log", append text name(table15)
	
	* Prep data
	use "$path_regressionData/allCompetitorPrices_P${sample}", replace
	sort id_nr_unique month
	gen D01_P = D_P
	replace D_P = -D_P if DL_P < 0
	gen DL_P_h24 = F23.P - L.P
	gen D_P_h24 = sign(DL_P_h24)
	gen D_P_up_h24 = 1 		if D_P_h24 == 1
	replace D_P_up_h24 = 0 	if inlist(D_P_h24, 0, -1)
	gen D_P_down_h24 = 1 	if D_P_h24 == -1
	replace D_P_down_h24 = 0 if inlist(D_P_h24, 0, 1)

	sort id_nr_unique month
	gen DP_month = month if D_P == 1
	by id_nr_unique (month): replace DP_month = DP_month[_n-1] if ~missing(DP_month[_n-1]) & missing(DP_month)
	gen priceAge = month - DP_month + 1
	gen L_xPriceAge = L.priceAge
	gen L_xLnPriceAge = log(L_xPriceAge)
	
	gen year = yofd(dofm(month))
	merge m:1 id_nr_unique year using "$path_intermediate/selection_revYwIdnr/revYwIdnr_allCats", keepusing(totRevYw_idnr) keep(match master) nogen
	rename totRevYw_idnr weight
	label variable weight "Total revenue of id_nr that year"

	merge m:1 month using "$rootL/External Data/Karadi2017.dta", keepusing(FF4_alt1_norm) keep(match master) nogen
	merge m:1 month using "$path_intermediate/US_variables.dta", keep(match master) nogen
	drop L7_* L8_* L9_* L10_* L11_* L12_*		
	gen month12 = month(dofm(month))
	
	* Regress
	reghdfe D_P_up_h24	L_xCompDem_P ebpnew c.L_xCompDem_P#c.ebpnew_residual L_xLnPriceAge L*_lip L*_lcpi L*_gs1 L*_ebpnew gs1 lip lcpi [aw=weight], absorb(id_nr month12) cluster(month category) poolsize(10)
	estimates save "$rootD/savedEstimations/1087u_h24${sample}", replace
	estimates save "$rootD/savedEstimations/Table15_C2${sample}", replace
	
	reghdfe D_P_down_h24	L_xCompDem_P ebpnew c.L_xCompDem_P#c.ebpnew_residual L_xLnPriceAge L*_lip L*_lcpi L*_gs1 L*_ebpnew gs1 lip lcpi [aw=weight], absorb(id_nr month12) cluster(month category) poolsize(10)
	estimates save "$rootD/savedEstimations/1087d_h24${sample}", replace
	estimates save "$rootD/savedEstimations/Table15_C4${sample}", replace
    
    * Close log
    log close table15
}
